package com.wooboo.dsp.redis.util;


import com.wooboo.server.ext.Config;
import com.wooboo.server.ext.Logger;
import com.wooboo.server.ext.util.NumberUtil;
import com.wooboo.server.ext.util.StringUtil;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisClient extends AbstractRedis {

	
	public static Logger logger = Logger.getLogger(RedisClient.class);
	private JedisPool jedisPool;//非切片连接池
	private static RedisClient redisClient;
	private String redis_ip=Config.getValue("redis.ips");
	private String redis_ports=Config.getValue("redis.ports");
	private String redis_authpass=Config.getValue("redis.authpass");
	
	private RedisClient() { 
        initialPool(); 
    } 
	

	public static RedisClient getRedisClient(){
        if(redisClient==null){
            synchronized (RedisClient.class) {
                if(redisClient==null) redisClient = new RedisClient();
            }
        }
        return redisClient;
    }
	
	 public Jedis getJedis() {
	        return jedisPool.getResource(); 
	 }

	 /**
	  * 初始化非切片池
	  */
	 private void initialPool() { 
	        // 池基本配置 
	        JedisPoolConfig config = new JedisPoolConfig(); 
	        config.setMaxTotal(300); 
	        config.setMaxIdle(100); 
	        config.setMaxWaitMillis(10000l); 
	        config.setTestOnBorrow(false); 
	        String ip = redis_ip.split(",")[0];
	        String port = redis_ports.split(",")[0];
	        if(StringUtil.isEmpty(redis_authpass)){
	        	 jedisPool = new JedisPool(config,ip,NumberUtil.getInteger(port),5000);
	        }else{
	        	 jedisPool = new JedisPool(config,ip,NumberUtil.getInteger(port),5000,redis_authpass.split(",")[0]);
	        }
	        
	 }

	 
	 
	 public static void main(String[] args){
		 RedisClient client = RedisClient.getRedisClient();
		 client.setex("note", 20, "haha");
		 
		 System.out.println(client.get("note"));
	 }


	@Override
	public void returnJedis(Jedis jedis) {
		jedisPool.returnResourceObject(jedis);		
	}



	 

}
